using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using PetShop.Domain.Services;
using NHibernate;
using PetShop.Domain.Factories;
using PetShop.Domain.Model;
using PetShop.Infrastructure.NHibernate;

namespace PetShop.Application.Facade
{
	public class OrderFC
	{


        public CartInfo AddItemToCart(CartInfo cart, string ItemId)
        {

           CartServices svc = new CartServices();

           svc.AddItemToCart(cart, ItemId);

           return cart;


        }

        public Order PrepareOrder(Account account,
                           CreditCardInfo creditcard,
                           AddressInfo billingAddress,
                           AddressInfo ShippingAddress,
                           CartInfo cart)
        {
            // create new order
            Order newOrder = OrderFactory.Create(account, creditcard, billingAddress, ShippingAddress, cart);
            return newOrder;

        }

     

        public void RegisterOrder(Order order)
        {

            ISession session = NHibernateHttpModule.CurrentSession;

          

       

            ITransaction tx = session.BeginTransaction();

            try
            {

                OrderServices svc = new OrderServices();

             
                svc.Register(order);

                
            

                tx.Commit();



            }
            catch (Exception)
            {
                if (tx != null) tx.Rollback();
                throw;

            }
            finally
            {
                if (tx != null) tx.Dispose();
            }


        }

     
   
	}
}
